/**
 * *************************************
 * Copyright(c)2016 Dingdang 
 * All Rights Reserved.
 *
 * @File: ResponseJson.java
 * @Package: com.nebula.util
 * @Description:
 * 
 * @Version: V1.0
 * @Author: Yidong.Xiang
 * @Date: 2017年8月18日 下午4:33:36
 * 
 * *************************************
 */
package com.ddyx.common.util;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: ResponseJson.java
 * @Module: 
 * @Description: 
 * 
 * @author: Yidong.Xiang
 * **@param <T>
 * @date: 2016年10月18日 下午4:33:36
 * 
 */
public class ResponseJson {
	private static final String STATUS = "status";
	private static final String MESSAGE = "message";
	private static final String PAGE = "page";
	private static final String SIZE = "size";
	private static final String TOTAL = "total";
	private static final String DATA = "data";
	
	private boolean status = true; 
	private String message = "";
	private static int page = 1;
	private static int size = 20;
	private static long total = 0;
	private static Map data = null;
	private static JSONObject jsonData = null;
	private static ResponseJson instance = null ;
	private ResponseJson()
	{

	}
	public static ResponseJson getInstance()
	{
		if(instance==null){
			instance = new ResponseJson();
		}
		//init
		data = new HashMap();
		data.put(DATA, new JSONArray());
		jsonData = new JSONObject();
		page = 1;
		size = 20;
		total = 0;
		return instance;
	}
	public ResponseJson setStatus(boolean status)
	{
		this.status= status;
		return instance;
	}
	public ResponseJson setMessage(String msg)
	{
		this.message= msg;
		return instance;
	}
	public ResponseJson setPage(int page)
	{
		this.page= page;
		return instance;
	}
	public ResponseJson setSize(int size)
	{
		this.size=size;
		return instance;
	}
	public ResponseJson setTotal(long total)
	{
		this.total=total;
		return instance;
	}
	public ResponseJson setData(Object o)
	{
		if(o!=null){
			this.data.put("data",o);
		}

		return instance;
	}
	public ResponseJson setData(List list)
	{
		if(list!=null){
			this.data.put("data",list);
		}

		return instance;
	}
	public ResponseJson setData(JSONArray data)
	{
		this.data.put("data",data);
		return instance;
	}
	public ResponseJson setDtata(JSONObject data)
	{
		JSONArray ja =new JSONArray();
		ja.add(data);
		this.data.put("data", ja);
		return instance;
	}
	public boolean getStatus()
	{
		return this.status;
	}
	public String getMessage()
	{
		return this.message;
	}
	public int getPage()
	{
		return this.page;
	}
	public int getSize()
	{
		return this.size;
	}
	public long getTotal()
	{
		return this.total;
	}
	public Map getData()
	{
		return this.data;
	}
	public JSONObject getJSONData()
	{
		this.jsonData.put(STATUS, status);
		this.jsonData.put(MESSAGE, message);
		this.jsonData.put(PAGE, page);
		this.jsonData.put(SIZE, size);
		this.jsonData.put(TOTAL, total);
		this.jsonData.put(DATA, data.get("data"));
		return this.jsonData;
	}
	public String toString()
	{
		return getJSONData().toString();
	}
}
